package cn.study.controller;


import cn.study.entity.StudyEmployee;
import cn.study.entity.StudyIntegral;
import cn.study.service.StudyEmployeeService;
import cn.study.service.StudyIntegralService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * <p>
 * 积分表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Slf4j
@Controller
@RequestMapping("/study-integral")
public class StudyIntegralController {
    @Autowired
    StudyIntegralService studyIntegralService;
    @Autowired
    StudyEmployeeService studyEmployeeService;
    @ResponseBody
    @GetMapping("/signin/{empid}")
    public String signin(@PathVariable("empid") Integer empid){
        /*先看看积分表里有没有员工
        * 传入员工id判断
        * */
        QueryWrapper<StudyIntegral> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("integral_emp_id",empid);
        Integer count=studyIntegralService.count(queryWrapper);
        if(count==0) {
            StudyIntegral studyIntegral = new StudyIntegral();
            studyIntegral.setIntegralEmpId(empid);
            studyIntegralService.save(studyIntegral);
        }
        String finallySigninTime=studyIntegralService.signin(empid);
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String signinTime = simpleDateFormat.format(date);
        if(finallySigninTime!=null&&finallySigninTime!="") {
            if (finallySigninTime.equals(signinTime)) {
                log.info("签到");
                return "今日已签到,不可重复签到";
            }
        }
        /*传入积分类
         * 判断员工id
         *
         * */
        log.info("签到");
        QueryWrapper<StudyIntegral> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("integral_emp_id",empid);

        StudyIntegral studyIntegral2 = studyIntegralService.getOne(queryWrapper1);
        int i=studyIntegral2.getIntegral()+2;
        studyIntegral2.setIntegral(i);
        studyIntegral2.setFinallySigninTime(new Date());
        studyIntegralService.update(studyIntegral2,queryWrapper);
        return "签到成功,积分+2";

    }
}

